jQuery(document).ready(function($) {
	$(document).on('click','.save_information',function(){
		var level = $('#izlevel').val();
		var time = $('#iztime').val();
		var requirements = $('#izrequirements').val();
		var note = $('#iznote').val();
		var post_id = $('#post_id').val();
		var author_id = $('#author_id').val();
		var numberRegex = /^[+-]?\d+(\.\d+)?([eE][+-]?\d+)?$/;
		if(time ==""){
			alert("Please insert time doing");
			return false;
		}else if(!numberRegex.test(time)){
			alert("Please insert number format");
			return false;
		}
		if(requirements == "" /*|| requirements.length <=6*/){
			alert("Please insert requirements for practice test");
			return false;
		}
		if(note=="" /*|| note.length <=6*/){
			alert("Please insert notes for practice test");
			return false;
		}
		$('.save_info').html('<img src="'+ajax_script.ajax_image+'ajax-loader.gif"/>').fadeIn(500);
		$.post(ajax_script.ajaxurl,{
			action: "savePracticeTestInformation",
			level: level,
			time: time,
			requirements: requirements,
			note: note,
			post_id: post_id,
			author_id: author_id
		},function(save_information){
			$('.save_info').fadeOut();
			if(save_information.success == true){
				$('#is_save').val("true");
				$("#practicetest_id").val(save_information.data);
				window.location.reload();
			}else{
				alert("Can't save, please checking");
				return false;
			}
		});
		return false;
	});

	$(document).on('click','.save_question',function(){
		var question = $(this).parent().next().val();
		var _this = $(this);
		if(question =="" /*|| question.length <=6*/){
			alert("Please insert your question");
			return false;
		}else{
			_this.parent().prev().html('<img src="'+ajax_script.ajax_image+'ajax-loader.gif"/>').fadeIn(500);
			$.post(ajax_script.ajaxurl,{
				action: "savePracticeTestQuestion",
				question: question,
				practicetest_id: $('#practicetest_id').val(),
				question_id:$(this).parent().next().next().next().val()
			},function(save_question){
				_this.parent().prev().fadeOut();
				if(save_question.success == true){
					_this.parent().next().next().next().val(save_question.data);
					window.location.reload();
				}else{
					alert("Can't save, please checking");
					return false;
				}
			});
		}
		return false;
	});

	$(document).on('blur','.izQuestion',function(){
		// Update code : 5/2/2014 - Editable
		if($(this).is('input')){
			var _this = $(this);
			var question = _this.val();
			var input_question = _this.next().next();
			if(question =="" /*|| question.length <=6*/){
				alert("Please insert your question");
				return false;
			}else{
				_this.parent().find('.save_question_db').html('<img src="'+ajax_script.ajax_image+'ajax-loader.gif"/>').fadeIn(500);
				$.post(ajax_script.ajaxurl,{
					action: "savePracticeTestQuestion",
					question: question,
					practicetest_id: $('#practicetest_id').val(),
					question_id:_this.next().next().val()
				},function(save_question){
					_this.parent().find('.save_question_db').fadeOut();
					if(save_question.success == true){
						_this.replaceWith($("<div id='izQuestion' class='izQuestion'>"+question+"</div>"));
						if(save_question.data > 0){
							input_question.val(save_question.data);
						}
					}else{
						alert("Can't save, please checking");
						return false;
					}
				});
			}
		}
	});

	$(document).on('click','.setInput',function(){
		var input = $(this).parent().find('.add');
		var input_val = input.val();
		var _this = $(this);
		if(input_val == "" /*|| input_val.length <=6*/){
			alert("Please insert your answer");
			return false;
		}else{
			$.post(ajax_script.ajaxurl,{
				action: "applyPracticeTestAnswer",
				answer: input_val,
				question_id:$(this).parent().parent().prev().val(),
				answer_id: input.next().val()
			},function(set_answer){
				if(set_answer.success == true){
					var new_input = _this.parent().find('.add');
					var new_input_val = new_input.val();
					new_input.attr('type','radio');
					new_input.val(new_input_val);
					_this.parent().find('.answer_id').val(set_answer.data);
					$('<span>'+new_input_val+'</span>').insertAfter(new_input);
					_this.removeClass('setInput').fadeOut().addClass('updateInput').text("+ Update").fadeIn();
				}else{
					alert("Can't apply answer, please checking");
					return false;
				}
			});
		}
		return false;
	});

	$(document).on('blur','input.izAddAnswer',function(){
		// Update code : 5/2/2014 - Editable
		var _this = $(this);
		if(_this.attr('type')=='text'){
			var input_val = _this.val();
			if(input_val == "" /*|| input_val.length <=6*/){
				alert("Please insert your answer");
				return false;
			}else{
				_this.parent().parent().parent().parent().find('.save_answer_db').html('<img src="'+ajax_script.ajax_image+'ajax-loader.gif"/>').fadeIn(500);
				$.post(ajax_script.ajaxurl,{
					action: "applyPracticeTestAnswer",
					answer: input_val,
					question_id:_this.parent().parent().parent().prev().val(),
					answer_id: _this.next().val()
				},function(set_answer){
					_this.parent().parent().parent().parent().find('.save_answer_db').fadeOut();
					if(set_answer.success == true){
						var new_input_val = _this.val();
						_this.attr('type','radio');
						_this.val(new_input_val);
						if(set_answer.data > 0){
							_this.next().val(set_answer.data);
						}
						var ext_dot = (new_input_val.length>100) ? "..." : "";
						_this.parent().parent().find('.updateInput').text(new_input_val.substring(0,100)+ext_dot).fadeIn();
					}else{
						alert("Can't apply answer, please checking");
						return false;
					}
				});
			}
		}
		return false;
	});

	$(document).on('click','.save_answer',function(){
		var _this = $(this);
		var findAnswer = _this.parent().next().next().find('input.izAddAnswer');
		var checked_box = false;
		if(_this.attr('data-question')=="true"){
			$(findAnswer).each(function(){
				if($(this).is(':checked')){
					checked_box = true;
					_this.prev().html('<img src="'+ajax_script.ajax_image+'ajax-loader.gif"/>').fadeIn(500);
					$.post(ajax_script.ajaxurl,{
						action: "applyPracticeTestAnswer",
						answer: "True/False",
						is_answer: $(this).val(),
						question_id: _this.parent().next().val(),
						answer_id: $(this).parent().parent().find('.answer_id').val()
					},function(save_answer){
						_this.prev().fadeOut();
						if(save_answer.success == true){
							window.location.reload();
						}
					});
				}
			});
			if(checked_box == false){
				alert("Please select correct answer");
				return false
			}
		}else{
			$(findAnswer).each(function(){
				if($(this).is(':checked')){
					checked_box = true;
					_this.prev().html('<img src="'+ajax_script.ajax_image+'ajax-loader.gif"/>').fadeIn(500);
					$.post(ajax_script.ajaxurl,{
						action: "applyPracticeTestAnswer",
						is_answer: 1,
						question_id:_this.parent().next().val(),
						answer_id: $(this).parent().parent().find('.answer_id').val(),
						answer: $(this).val(),
					},function(save_answer){
						_this.prev().fadeOut();
						if(save_answer.success == true){
							window.location.reload();
						}
					});
				}
			});
			if(checked_box == false){
				alert("Please select correct answer");
				return false
			}
		}
		return false;
	});

	$(document).on('click','.remove_question',function(){
		var _this = $(this);
		var is_save = _this.parent().next().next().next().val();
		if(is_save == ""){
			_this.parent().parent().fadeOut(500,function(){
				_this.parent().parent().remove();
			});
		}else{
			_this.parent().prev().html('<img src="'+ajax_script.ajax_image+'ajax-loader.gif"/>').fadeIn(500);
			$.post(ajax_script.ajaxurl,{
				action: "removePracticeTestQuestion",
				question_id:_this.parent().next().next().next().val(),
			},function(remove_question){
				_this.parent().prev().fadeOut();
				if(remove_question.success == true){
					_this.parent().parent().fadeOut(500,function(){
						_this.parent().parent().remove();
					});
				}
			});
		}
		return false;
	});
});